<?php
$pageTitle ="Login Page";
include "common/header.php";
include "config.php";

$objLoginForm = new BM_PHPForm();
$objLoginForm->UseLocator();
$objLoginForm->SetIndent("        ");


if($objLoginForm->IsPost()){
$objLoginForm->ChEmptiness("username","Username Is Empty.");
$objLoginForm->ChWithRegex("username", "Username Have Invalid Chars.", "/^([a-zA-Z0-9_\\.\\-])*$/");
$objLoginForm->ChEmptiness("password","Password Is Empty.");
	if($objLoginForm->IsValid()){
		$strUsername = $objLoginForm->arrPostData['username'];	
		$entUser = UserController::find($strUsername);
		if ($entUser->username == "guest"){
			$objLoginForm->SetError("username","Username Or Password Is Incorrect.");
		}else{
			if ($entUser->password == md5($objLoginForm->arrPostData['password'])){
				$entUser = UserController::findUserRoles($entUser);
				$_SESSION['X_USER'] = $entUser;
				header("Location: index.php");
			}else{
			$objLoginForm->SetError("username","Username Or Password Is Incorrect");
			}
		}
	}
}
print $objLoginForm->ErrMsgs();
print $objLoginForm->Open("loginform","Login Form");
print $objLoginForm->TextBox("username","Username:","");
print $objLoginForm->TextBox("password","Password:","",array(),"password");
print $objLoginForm->SubmitButton("submit","Login");
print $objLoginForm->Close();

include "common/footer.php";
?>